﻿package app.view.components.guiDima 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * Переключатель по страницам (по страницам когда)
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class NavigatorPage extends Sprite
	{
		
		//	public
		
		//	private
		private var _countPage:int;
		private var _items:/*PageBtnD*/Array;
		private var _current:PageBtnD;
		//	const
		private static const GAP:Number = 5;
		
		//	CONSTRUCTOR
		public function NavigatorPage() 
		{
			init();
		}
		
		//	METHODS
		//	private
		private function init():void
		{
			_items = new Array();
			
			//graphics.beginFill(0xff0000,0.5);
			//graphics.drawRect(0, 0, 100, 20);
			//graphics.endFill();
		}
		
		private function build():void
		{
			for (var i:int = 0; i < _countPage; i++) 
			{
				var item:PageBtnD = new PageBtnD(i);
				item.addEventListener(Event.CHANGE, onChangePageBtn);
				addChild(item);
				_items.push(item);
			}
			
			allocate();
		}
		
		private function allocate():void
		{
			for (var i:int = 0; i < _items.length; i++) 
			{
				if (i!=0) 
				{
					_items[i].x = _items[i - 1].x + _items[i - 1].width + GAP;
				}
			}
			
		}
		//	public
		public function destroy():void
		{
			for (var i:int = 0; i < _items.length; i++) 
			{
				_items[i].destroy();
				_items[i].removeEventListener(Event.CHANGE, onChangePageBtn);
				removeChild(_items[i]);
			}
		}
		
		public function updateDisplayList(sw:Number,sh:Number):void
		{
			
		}
		
		public function setSelected(ind:int):void
		{
			if (_current==null) 
			{
				_current = _items[ind];
				_current.selected = true;
			}else
			{
				_current.selected = false;
				_current = _items[ind];
				_current.selected = true;
			}
		}
		//	----------------------------------------------------------------------------------
		
		//	GETTERS/SETTERS
		public function get countPage():int { return _countPage; }
		
		public function set countPage(value:int):void 
		{
			_countPage = value;
			
			build();
		}
		
		public function get current():PageBtnD { return _current; }
		
		//	----------------------------------------------------------------------------------
		
		//	HANDLERS
		private function onChangePageBtn(e:Event):void 
		{
			////trace('sadfga',PageBtnD(e.target).id);
			setSelected(PageBtnD(e.target).id);
			
			var ev:Event= new Event(Event.CHANGE);
			dispatchEvent(ev);
		}
		
	}

}